//
//  HMHomeViewModel.swift
//  WeiBo
//
//  Created by Apple on 2017/5/31.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
//  封装首页网络请求
class HMHomeViewModel: NSObject {

    //  微博数据模型数组
    lazy var statusList: [HMStatusViewModel] = [HMStatusViewModel]()
    
    func loadData(callBack: @escaping (Bool)->()) {
        HMNetworTools.sharedTools.requestStatuses(accessToken: HMUserAccountViewModel.sharedUserAccount.accessToken!) { (response, error) in
            if error != nil {
                print("网络异常: \(error!)")
                callBack(false)
                return
            }
            
            let dic = response as! [String: Any]
            //  微博的字典数组
            let dicArray = dic["statuses"] as! [[String: Any]]
            //  使用YYModel完成字典转模型
            let modelArray = NSArray.yy_modelArray(with: HMStatus.self, json: dicArray) as! [HMStatus]
            var tempArray = [HMStatusViewModel]()
            for model in modelArray {
                let statusViewModel = HMStatusViewModel()
                //  设置模型
                statusViewModel.status = model
                tempArray.append(statusViewModel)
            }
            //  记录数据源
            self.statusList = tempArray
            //  重新加载数据
            callBack(true)
        }
    }
    
}
